package com.jyu.apts.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.jyu.apts.dto.ItemProcessRecordInfoDto;
import com.jyu.apts.dto.ResultDto;
import com.jyu.apts.dto.TableDto;
import com.jyu.apts.entity.ItemProcessRecord;
import com.jyu.apts.entity.ItemProcessRecordInfo;
import com.jyu.apts.service.ItemProcessRecordInfoService;
import com.jyu.apts.service.ItemProcessRecordService;
import com.jyu.apts.utils.AliyunOSSUtil;

@RestController
public class ItemProcessRecordInfoController {
	@Autowired
	ItemProcessRecordService itemProcessRecordService;
	@Autowired
	ItemProcessRecordInfoService itemProcessRecordInfoService;
	
	@RequiresPermissions("itemProcessRecordInfo_html")
	@RequestMapping("/itemProcessRecordInfo_html")
	public ModelAndView itemProcessRecordInfo_html() {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("/itemProcessRecordInfo.html");
		return mv;
	}
	
	@RequiresPermissions("itemProcessRecordInfo_html")
	@RequestMapping("/addItemProcessRecordInfo")
	public ResultDto addItemProcessRecordInfo(long itemProcessRecordId,String title,String description,MultipartFile[] files) throws IOException {
		ItemProcessRecord itemProcessRecord = itemProcessRecordService.getOne(itemProcessRecordId);
		if(itemProcessRecord == null) {
			return new ResultDto("流程记录不存在",null,2);
		}
		ItemProcessRecordInfo itemProcessRecordInfo = new ItemProcessRecordInfo();
		itemProcessRecordInfo.setTitle(title);
		itemProcessRecordInfo.setDescription(description);
		if(files!=null && files.length>0) {
			StringBuilder sb = new StringBuilder("");
			for(MultipartFile file : files) {
				String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf('.') + 1);
				String objectName = AliyunOSSUtil.upload(file.getInputStream(), suffix);
				sb.append("#");
				sb.append("/oss/");
				sb.append(objectName);
			}
			itemProcessRecordInfo.setUrls(sb.toString().substring(1));
		}
		return new ResultDto("添加流程记录信息成功",null,0);
	}
	
	@RequiresPermissions("itemProcessRecordInfo_html")
	@RequestMapping("/getItemProcessRecordInfoPage")
	public TableDto<ItemProcessRecordInfoDto> getItemProcessRecordInfoPage(Long organizationId,Integer pageNumber,Integer pageSize,String search) {
		Page<ItemProcessRecordInfo> pageResult = itemProcessRecordInfoService.getItemProcessRecordInfoPage(pageNumber, pageSize, search,organizationId);
		List<ItemProcessRecordInfo> itemProcessRecordInfos = pageResult.getContent();
		List<ItemProcessRecordInfoDto> itemProcessRecordInfoDtos = new ArrayList<ItemProcessRecordInfoDto>();
		itemProcessRecordInfos.forEach(itemProcessRecordInfo->itemProcessRecordInfoDtos.add(new ItemProcessRecordInfoDto().convert(itemProcessRecordInfo)));
		return new TableDto<ItemProcessRecordInfoDto>(itemProcessRecordInfoDtos,pageResult.getTotalElements());
	}
}
